package com.bocai.dahuatech.ui.activity;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bocai.dahuatech.Bean.AddressListBean;
import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.ui.adapter.MyAddressAdapter;
import com.bocai.dahuatech.utils.AESUtils;
import com.bocai.dahuatech.utils.SharePrefencesUtil;
import com.google.gson.Gson;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MyAddressActivity extends BaseActivity implements AdapterView.OnItemClickListener
{

    @Bind(R.id.lv_myaddress)
    ListView lvAddress;
    AddressListBean bean;
    ArrayList<AddressListBean.ContentEntity> datas=new ArrayList<>();
    MyAddressAdapter adapter;
    String what;
    @Override
    protected int contentView()
    {
        return R.layout.activity_my_address;
    }

    @Override
    protected void initView()
    {
        ButterKnife.bind(this);
        initToolbar(R.string.addrdetail);
        what=getIntent().getStringExtra("what");
        mToolbar.inflateMenu(R.menu.menu_addaddress);
        initEvent();


    }

    @Override
    protected void onResume()
    {
        super.onResume();
        getData();
    }

    private void getData()
    {
        showLoading();
        getTestApi().getAddrList(SharePrefencesUtil.getCustomerCode(this))
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    Observer<BaseBean> observer=new Observer<BaseBean>()
    {
        @Override
        public void onCompleted()
        {

        }

        @Override
        public void onError(Throwable e)
        {
            hideLoading();
            showToast(R.string.neterror);
            Log.e("error",e.toString());
        }

        @Override
        public void onNext(BaseBean baseBean)
        {
            hideLoading();
            if(baseBean.getReturnNo().equals("1"))
            {
                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result",result);
                bean=new Gson().fromJson(result,AddressListBean.class);
                datas.addAll(bean.getContent());
                adapter=new MyAddressAdapter(MyAddressActivity.this,bean.getContent());
                lvAddress.setAdapter(adapter);
            }else
            {
                showToast(baseBean.getReturnInfo().toString());
            }
        }
    };
    @Override
    protected void initEvent()
    {
        super.initEvent();
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener()
        {
            @Override
            public boolean onMenuItemClick(MenuItem item)
            {
                startActivity(new Intent(MyAddressActivity.this, AddAddressActivity.class));
                return false;
            }
        });
        lvAddress.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        AddressListBean.ContentEntity data= (AddressListBean.ContentEntity) parent.getAdapter().getItem(position);
        if(what.equals("list"))
        {
            Intent intent=new Intent(this,AddressDetailActivity.class);
            intent.putExtra("data",data);
            startActivity(intent);
        }else if (what.equals("choose"))
        {
            Intent intent=new Intent();
            intent.putExtra("data",data);
            setResult(2000,intent);
            finish();
        }
    }
}
